<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\BaseCourseCategoryModel;
use app\model\CategoryModel;
use app\model\RedisModel;
use think\App;
use think\facade\Db;

class BaseCourseCategory extends Base {

    protected $middleware = [Auth::class];
    private $baseCourseCategoryModel;

    public $cacheKey="base:course:category:id:";

    private $req;

    private $page;

    private $limit;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 999999;

        $this->baseCourseCategoryModel = new BaseCourseCategoryModel();

        $this->isLogin();
    }

    /*
     * 获取店铺列表
     * */
    public function getList(){

        $list = $this->baseCourseCategoryModel->getList([],$this->page,$this->limit);

        $total = $this->baseCourseCategoryModel->getTotal("","");

        return $this->succData($total,$list);

    }


    /*
    * 新增
    * */
    public function add()
    {
        if(!isset($this->req['base_course_category_name']) || empty($this->req['base_course_category_name'])){
            return $this->fail("请填写授课形式名称");
        }


        $res = $this->baseCourseCategoryModel->insertGetId([
            "base_course_category_name"=>$this->req['base_course_category_name']
        ]);
        if($res){
            return $this->succ();
        }else{
            return $this->fail();
        }
    }


    /*
  * 新增
  * */
    public function edit()
    {
        if(!isset($this->req['base_course_category_id']) || empty($this->req['base_course_category_id'])){
            return $this->fail("请填写授课形式名称");
        }

        if(!isset($this->req['base_course_category_name']) || empty($this->req['base_course_category_name'])){
            return $this->fail("请填写授课形式名称");
        }

        if(!isset($this->req['base_course_category_status']) || !in_array($this->req['base_course_category_status'],[-1,1])){
            return $this->fail("请选择状态");
        }


        $res = $this->baseCourseCategoryModel->where("base_course_category_id",$this->req['base_course_category_id'])->save([
            "base_course_category_name"=>$this->req['base_course_category_name'],
            "base_course_category_status"=>$this->req['base_course_category_status']
        ]);
        if($res){
            $this->delCache($this->req['base_course_category_id']);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }


    public function del()
    {
        if(!isset($this->req['base_course_category_id']) || empty($this->req['base_course_category_id'])){
            return $this->fail("请填写授课形式名称");
        }


        $res = $this->baseCourseCategoryModel->where("base_course_category_id",$this->req['base_course_category_id'])->save([
            "is_delete"=>1
        ]);
        if($res){
            $this->delCache($this->req['base_course_category_id']);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }

    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }


    public function delAllCache()
    {
        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }
}
